﻿using System;
using Microsoft.Office.Interop.Excel;

namespace FlyData
{
    class FormatMessage
    {
        private readonly string _messageHeader;
        private readonly Range _rowsHeader = null;
        private readonly Range _rowsMessageBody;
        private readonly string _messageFooter;

        public FormatMessage(string messageHeader, Range rowsMessageBody, string messageFooter)
        {
            _messageHeader = messageHeader;
            _rowsMessageBody = rowsMessageBody;
            _messageFooter = messageFooter;
        }

        public FormatMessage(string messageHeader, Range rowsHeader, Range rows, string messageFooter)
        {
            _messageHeader = messageHeader;
            _rowsHeader = rowsHeader;
            _rowsMessageBody = rows;
            _messageFooter = messageFooter;
        }

        public String GetMessageInHtmLFormat()
        {
            return _messageHeader  + FormatRangeToHtmlTable(_rowsMessageBody) + _messageFooter;
        }

        private string FormatRangeToHtmlTable(Range messageBody)
        {
            string htmlMessage = "<table border='8'>";
            htmlMessage = AddHeaderRowToHtml(htmlMessage);
            foreach (Range row in messageBody.Rows)
            {
                htmlMessage = htmlMessage + "<tr>";
                htmlMessage = AddRowToHtml(htmlMessage, row);
                htmlMessage = htmlMessage + "</tr>";
            }
            htmlMessage = htmlMessage + "</table>";
            return htmlMessage;
        }

        private string AddHeaderRowToHtml(string htmlMessage)
        {
            if (_rowsHeader != null)
            {
                htmlMessage = AddRowToHtml(htmlMessage, _rowsHeader);
            }
            return htmlMessage;
        }

        private static string AddRowToHtml(string htmlMessage, Range row)
        {
            foreach (Range cell  in row.Cells)
            {
                htmlMessage = htmlMessage + "<td>" + cell.Value2 + "</td>";
            }
            return htmlMessage;
        }
    }
}
